<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/4/3
 * Time: 14:30
 */

namespace Admin\Controller;


use Admin\Model\ImageReplyLogic;
use Think\Controller;
use Think\Exception;
use Wechat\Model\WxLogic;

class ImageReplyApiController extends Controller
{

    /**
     * 添加图片回复规则
     */
    public function add_img_rule()
    {
        /*
         * 雷区 缺少这句话会报 Class 'Wechat\Model\WxLogic' not found
         * 原因 未知
         */
        $logic = new WxLogic();
        WxLogic::getAccessToken();

        $logic = new ImageReplyLogic();
        $rid = I('rid');
        if (empty($rid)) {
            $logic->add_image_reply_rule();
        } else {
            $logic->update_image_reply_rule();
        }

    }

    /**
     * 获取图片回复规则列表
     */
    public function get_image_reply_list()
    {
        $key_word = I('key_word');
        $db_reply_rule = D('ReplyRule');
        $db_image_rule = D('Image');

        if (!empty($key_word)) {
            $where['rule_name'] = array('like', '%' . $key_word . '%');
        }
        $where['moudle'] = 'image';

        //根据rule_id分组 获取所有的rule_id
        $temp = $db_reply_rule
            ->field('rule_id')
            ->where($where)
            ->group('rule_id')
            ->page(I('page'), 15)
            ->select();
        //生成 rule_id 数组 用于 in 查询
        $rule_ids = array();
        foreach ($temp as $key => $id) {
            array_push($rule_ids, $id['rule_id']);
        }
        //获取最大页数
        $max_page = ceil(count($db_reply_rule
                ->field('rule_id')
                ->where($where)
                ->group('rule_id')
                ->select()) / 15);
        $result_data['max_page'] = $max_page;

        // 查询15条在rule_ids内的记录
        $where['rule_id'] = array('IN', $rule_ids);
        $reply_rules = $db_reply_rule
            ->where($where)
            ->select();

        //通过rule_id合并key_words
        $rules_datas = array();
        foreach ($reply_rules as $reply_rule) {
            if (empty($rules_datas[$reply_rule['rule_id']])) {
                $rules_datas[$reply_rule['rule_id']] = $reply_rule;
            } else {
                $rules_datas[$reply_rule['rule_id']]['rule_keywords'] .= ',' . $reply_rule['rule_keywords'];
            }
        }
        //查询另一张表 删除不要的查询条件
        unset($where['moudle']);
        $image_rules = $db_image_rule
            ->where($where)
            ->select();

        //通过rule_id拼接两张表数据
        foreach ($image_rules as $key => $image_rule) {
            $rules_datas[$image_rule['rule_id']] = array_merge($rules_datas[$image_rule['rule_id']], $image_rule);
        }
        $result_data['rules'] = $rules_datas;
        \Common\Common\print_json_data(200, $result_data, '查询成功');
    }

    /**
     * 获取单条图片回复规则
     */
    public function get_image_reply_rule()
    {
        $rid = I('rid');
        if (empty($rid)) {
            \Common\Common\request_parameter_invalid();
            exit;
        }
//        $db_view = M('wx_reply_rule')
//            ->alias('r')
//            ->join('left join wx_images_reply as i on r.rule_id = i.rule_id');
//        $result = $db_view->getById($rid);
        $db_reply_rule = D('ReplyRule');
        $db_image_rule = D('Image');
        $where['rule_id'] = $rid;

        $reply_rules = $db_reply_rule->where($where)->select();
        foreach ($reply_rules as $rule) {
            if (empty($result)) {
                $result = $rule;
            } else {
                $result['rule_keywords'] .= ',' . $rule['rule_keywords'];
            }
        }
        $image_rules = $db_image_rule->where($where)->find();

        \Common\Common\print_json_data(200, array_merge($result, $image_rules), '查询成功');
    }

    /**
     * 修改图片回复状态
     */
    public function change_image_reply_status()
    {
        $rid = I('rid');
        $status = I('status');
        if (empty($status)) {
            \Common\Common\request_parameter_invalid();
            exit;
        }
        if ($status == 'ACTIVE') {
            $where['rule_status'] = 'NOACTIVE';
        } else {
            $where['rule_status'] = 'ACTIVE';
        }
        $where['id'] = $rid;
//        D('ReplyRule')
//            ->save($where);
        \Common\Common\print_json_data(200, D('ReplyRule')->save($where), '信息修改成功');
    }

    /**
     * 删除图片回复规则
     */
    public function remove_rules()
    {
        $rid = I('rid');
        if (empty($rid)) {
            \Common\Common\request_parameter_invalid();
            exit;
        }
        \Common\Common\print_json_data(200, D('ReplyRule')->delete($rid), '删除成功');
    }

}